package com.pdg.mcplugin.invisiblehand.commandprocessors;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.pdg.mcplugin.common.ArgumentList;
import com.pdg.mcplugin.common.baseclasses.CommandProcessorBase;
import com.pdg.mcplugin.invisiblehand.InvisibleHand;
import com.pdg.mcplugin.invisiblehand.dataproviders.CommodityKey;

public class RemoveCommandProcessor extends CommandProcessorBase<InvisibleHand> {

	private static final String messageSuccess = "Removed commodity '%s' from world '%s'.";
	private static final String messageFailure = "Could not remove commodity '%s' from world '%s'.";

	public RemoveCommandProcessor(InvisibleHand thePlugin) {
		super(thePlugin, new String[]{
				"/ih remove - help with the remove command",
				"/ih remove (commodity-name) - removes (commodity-name) from the current world",
				"/ih remove (commodity-name) (world name) - removes (commodity-name) from (world name)"
		});
	}

	@Override
	protected boolean specialProcessCommand(CommandSender theCommandSender,
			String theSubCommand, ArgumentList theArgumentList) {
		String theCommodityName = theSubCommand;
		String theWorld = null;
		if(theCommandSender instanceof Player){
			Player thePlayer = (Player)theCommandSender;
			theWorld = thePlayer.getWorld().getName();
		}
		if(theWorld==null && theArgumentList.size()<1){
			//TODO message that argument list is too small
			return false;
		}
		if(theWorld==null){
			while(theArgumentList.size()>0){
				if(theWorld==null){
					theWorld = theArgumentList.pop();
				}else{
					theWorld += " ";
					theWorld += theArgumentList.pop();
				}
			}
		}
		CommodityKey theKey = new CommodityKey(theCommodityName,theWorld);
		if(getPlugin().getDataProvider().getCommodityTableProvider().delete(theKey)){
			theCommandSender.sendMessage(String.format(messageSuccess,theCommodityName,theWorld));
		}else{
			theCommandSender.sendMessage(String.format(messageFailure,theCommodityName,theWorld));
		}
		return true;
	}

}
